package com.chb.test.BuyTicketsDemo_12;

public class Windows extends Thread{
    public Windows() {
    }

    public Windows(String name) {
        super(name);
    }
    int flag = 0;
    @Override
    public void run() {
        while (true) {
            synchronized (Tickets.obj) {
                if(Tickets.count == 0) {
                    break;
                } else {
                    if(flag == 0) {
                        try {
                            sleep(500);
                            flag = 1;
                            Tickets.count --;
                            System.out.println(getName() + "卖出了一张票，还剩余" + Tickets.count + "张票待卖出");
                            Tickets.obj.notifyAll();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        flag = 0;
                        try {
                            Tickets.obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }
    }
}
